//
//  JHStatusOrginView.swift
//  JHSinaWeibo
//
//  Created by apple on 16/6/3.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

//设置对齐的边距
let StatusCellMargin:CGFloat = 8
class JHStatusOrginView: UIView {
    var statusViewModel : JHStatusViewModel?{
        didSet{
        //数据绑定
            let url = NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? "")
            headIcon.sd_setImageWithURL(url)
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.status?.created_at
            soureLabel.text = statusViewModel?.status?.source
            contentLabel.text = statusViewModel?.status?.text
            //认证图片
            if statusViewModel?.status?.user?.verified == 1 {
                //认证用户
                verifiedImageiew.hidden = false
            }
            else{
                verifiedImageiew.hidden = true
                }
            //用户等级
            if statusViewModel?.status?.user?.mbrank > 0 && statusViewModel?.status?.user?.mbrank < 7 {
                let image = UIImage(named: "common_icon_membership_level\(statusViewModel?.status?.user?.mbrank ?? 0)")
                mbrankImageView.image = image
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.yellowColor()
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

//设置ui界面
private func setUpUI(){
    addSubview(headIcon)
    addSubview(verifiedImageiew)
    addSubview(mbrankImageView)
    addSubview(timeLabel)
    addSubview(soureLabel)
    addSubview(nameLabel)
    addSubview(contentLabel)
    //设置y约束
    headIcon.snp_makeConstraints { (make) in
        make.top.left.equalTo(self).offset(StatusCellMargin)
        make.height.equalTo(40)
        make.width.equalTo(40)
    }
    verifiedImageiew.snp_makeConstraints { (make) in
        make.bottom.right.equalTo(headIcon)
    }
    mbrankImageView.snp_makeConstraints { (make) in
        make.top.equalTo(headIcon.snp_top)
    make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
    }
    timeLabel.snp_makeConstraints { (make) in
        make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
        make.bottom.equalTo(headIcon.snp_bottom)
    }
    soureLabel.snp_makeConstraints { (make) in
        make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        make.bottom.equalTo(headIcon.snp_bottom)
    }
    contentLabel.snp_makeConstraints { (make) in
        make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
        make.left.equalTo(headIcon.snp_left)
    }
    nameLabel.snp_makeConstraints { (make) in
        make.top.equalTo(headIcon.snp_top)
        make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
    }
    //给原创微博底部设置添加约束
    self.snp_makeConstraints { (make) in
        make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
    }
}
//懒加载子视图
private lazy var headIcon:UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    //认证头像
    private lazy var verifiedImageiew:UIImageView = UIImageView(image:UIImage(named:"avatar_vip"))
    //用户等级
    private lazy var mbrankImageView:UIImageView = UIImageView(image:UIImage(named:"common_icon_membership"))
    //用户发表时的时间
    private lazy var timeLabel:UILabel = UILabel(title:"10:10",font: 10,textColor: UIColor.orangeColor())
    //微博来源
    private lazy var soureLabel:UILabel = UILabel(title:"你懂的",font: 10,textColor: UIColor.darkGrayColor())
    //微博昵称
    private lazy var nameLabel:UILabel = UILabel(title:"大哥大",font: 14,textColor: UIColor.darkGrayColor())
    //微博内容
    private lazy var contentLabel:UILabel = UILabel(title:"昵称无处单位 iviu 我和 vu 个 vui 大户恶化及地产股我 iu 都很粗很好i 今晚看吹风我的擦苏废弃物 v 个 v",font: 14,textColor: UIColor.redColor(),aliginment: .Left)
    
}